import { createSlice } from "@reduxjs/toolkit";

// 创建 slice 对象
let slice = createSlice({
  //状态的名字
  name: 'count',
  //状态的初始值
  initialState: 100,
  //配置 reducer 的功能
  reducers: {
    add: (state, action) => {
      return state + action.payload;
    },
    sub: (state, action) => {
      return state - action.payload;
    }
  }
});

//获取 slice 内部的 reducer 函数
let reducer = slice.reducer;

//获取同步 action creator
export let {add, sub} = slice.actions;

//暴露
export default reducer;

//声明异步的 action creator
export let asyncAddAction = (payload) => {
  return dispatch => {
    //定时器
    setTimeout(() => {
      dispatch(add(payload));
    }, 1000)
  }
}